package org.shapeoforion.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * This class is (yet another) wrapper around commons-logging.
 *
 * The reason for this wrapper to be present is that logging (in general) can be enabled
 * or disabled by setting a flag (public static boolean). If logging is disabled, by setting the
 * flag to false, the Java compiler and interpreter will not even bother to do the logging at all,
 * therefore <em>no</em> performance penalty occurs.
 *
 * At least, that's the theory :-)
 *
 * @author jps
 * @version 1.0
 */
public class MyLogger implements Log {

    /**
     * Force usage of getInstance().
     *
     * @param clazz the class to generate the logger for
     */
    private MyLogger(Class clazz) {

        log = LogFactory.getLog(clazz);
    }

    /**
     * Get a logger instance for the specified class.
     *
     * @param clazz the class to generate the logger for
     * @return a logging instance
     */
    public static MyLogger getInstance(Class clazz) {

        return new MyLogger(clazz);
    }

    private Log log = null;

    /** toggle logging. */
    private static final boolean LOGGING_ENABLED = true;

    public void debug(Object object) {

        if(LOGGING_ENABLED) log.debug(object);
    }

    public void debug(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.debug(object, throwable);
    }

    public void error(Object object) {
        if(LOGGING_ENABLED) log.error(object);
    }

    public void error(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.error(object, throwable);
    }

    public void fatal(Object object) {
        if(LOGGING_ENABLED) log.fatal(object);
    }

    public void fatal(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.fatal(object, throwable);
    }

    public void info(Object object) {
        if(LOGGING_ENABLED) log.info(object);
    }

    public void info(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.info(object,throwable);
    }

    public boolean isDebugEnabled() {
        return LOGGING_ENABLED ? log.isDebugEnabled() : false;
    }

    public boolean isErrorEnabled() {
        return LOGGING_ENABLED ? log.isErrorEnabled() : false;
    }

    public boolean isFatalEnabled() {
        return LOGGING_ENABLED ? log.isFatalEnabled() : false;
    }

    public boolean isInfoEnabled() {
        return LOGGING_ENABLED ? log.isInfoEnabled() : false;
    }

    public boolean isTraceEnabled() {
        return LOGGING_ENABLED ? log.isTraceEnabled() : false;
    }

    public boolean isWarnEnabled() {
        return LOGGING_ENABLED ? log.isWarnEnabled() : false;
    }

    public void trace(Object object) {
        if(LOGGING_ENABLED) log.trace(object);
    }

    public void trace(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.trace(object, throwable);
    }

    public void warn(Object object) {
        if(LOGGING_ENABLED) log.warn(object);
    }

    public void warn(Object object, Throwable throwable) {
        if(LOGGING_ENABLED) log.warn(object, throwable);
    }
}
